/**
  ******************************************************************************
  * @file           : TokenManager.h
  * @author         : livio[yuanfeng1897@outlook.com]
  * @brief          : None
  * @attention      : None
  * @date           : 2023/3/2
  ******************************************************************************
  */
#ifndef CPPDEMO_TOKENMANAGER_H
#define CPPDEMO_TOKENMANAGER_H

#include <string>
#include <unordered_set>
#include <unordered_map>
#include <mutex>

class TokenManager final {
public:
    static TokenManager &instance();

    /**
     * generate token by client id
     * @param id
     * @param dur  有效时间 days
     * @return
     */
    auto generateToken(const std::string &id, std::chrono::days dur = std::chrono::days{-1}) -> std::string;

    /**
     * destroy token
     * @param token
     */
    auto destroyToken(const std::string &token) -> void;

    /**
     * Whether the token exists
     * @param token
     * @return
     */
    auto exist(const std::string &token) -> bool;

    /**
     * Whether the token is legitimate
     * @param token
     * @return
     */
    auto valid(const std::string &token) -> bool;

protected:
private:
    TokenManager() = default;
    ~TokenManager() = default;

    std::mutex tokenSetMtx;
    std::unordered_set<std::string>  tokenSet;
};

#endif //CPPDEMO_TOKENMANAGER_H
